//
//  WBStatusListModelView.m
//  TransWB
//
//  Created by 孝飞王 on 2016/12/30.
//  Copyright © 2016年 孝飞王. All rights reserved.
//

#import "WBStatusListModelView.h"
#import "WBStatuse.h"
@implementation WBStatusListModelView

- (void)loadStatusIsPull:(BOOL)isPull isSuccess:(void(^)(BOOL isSuccess))complet{
    
    int64_t since_id =  isPull ? 0 : ([self.statusDataSource.firstObject id] ?: 0);
    int64_t max_id =   isPull ? ([self.statusDataSource.lastObject id] - 1  ?: 0) : 0;
    
    NSDictionary *param = @{@"since_id":@(since_id),@"max_id":@(max_id)};
    [[WBNetworkManager shareManager] statustListParam:param completion:^(NSArray *list, BOOL isSuccess) {
        
        NSLog(@"%@",list);

        if (list == nil) {
            complet(isSuccess);
            return ;
        }
        
        //解析模型数组
        NSArray *stausList = [NSArray yy_modelArrayWithClass:[WBStatuse class] json:list];
        
        if (stausList == nil || stausList.count == 0) {
            complet(isSuccess);
            return;
        }
        NSLog(@"加载了%ld个数据",stausList.count);
        
        isPull ? [self addModelArrayBehind:stausList] : [self addModelArrayAhead:stausList];
        complet(isSuccess);
    }];
}

//下拉刷新 加载数据
- (void)addModelArrayAhead:(NSArray*)array{
    for (NSInteger i =  array.count  - 1;i >= 0;i--)
        [self.statusDataSource insertObject:array[i] atIndex:0];
}

//上拉刷新数据
- (void)addModelArrayBehind:(NSArray*)array{
    [self.statusDataSource addObjectsFromArray:array];
}




















#pragma mark - LazyLoad - DataSourceArray
- (NSMutableArray *)statusDataSource{
    
    if (_statusDataSource == nil)
        _statusDataSource = [NSMutableArray array];
    return _statusDataSource;
}


@end
